package tt.com.warehousemanager.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tt.com.common.model.YxCommonProviders;
import tt.com.common.model.YxCommonWarehouseGoods;
import tt.com.common.util.PageUtil;
import tt.com.warehousemanager.mapper.WarehouseGoodsMapper;
import tt.com.warehousemanager.service.WarehouseGoodsService;

import java.util.Date;

/**
 * @author Czycka
 * @date 2022/8/18 17:49
 * @description 仓库物品服务实现类
 */
@Service
public class WarehouseGoodsServiceImpl extends ServiceImpl<WarehouseGoodsMapper, YxCommonWarehouseGoods> implements WarehouseGoodsService {

    @Autowired
    private WarehouseGoodsMapper mapper;

    @Override
    public Integer insertGoods(YxCommonWarehouseGoods goods, String createUserId, YxCommonProviders provider) {
        // 设置创建时间
        goods.setCreateTime(new Date());
        // 设置创建操作人id
        goods.setCreateUserId(createUserId);
        // 绑定服务项目
        goods.setTargetId(String.valueOf(provider.getId()));
        return baseMapper.insert(goods);
    }

    @Override
    public Integer deleteGoods(Long id, String deleteUserId) {
        YxCommonWarehouseGoods goods = baseMapper.selectById(id);
        if (goods == null) {
            // 若不存在 id 对应的数据则返回 -1
            return -1;
        }
        if (goods.getDataStatus() == 1) {
            // 若数据已被删除则返回 -2
            return -2;
        } else {
            // 设置删除时间
            goods.setDeleteTime(new Date());
            // 设置删除操作人id
            goods.setDeleteUserId(deleteUserId);
            baseMapper.updateById(goods);
            return baseMapper.deleteById(goods.getId());
        }
    }

    @Override
    public Integer updateGoods(YxCommonWarehouseGoods goods, String updateUserId) {
        // 设置修改时间
        goods.setLastUpdateTime(new Date());
        // 设置修改操作人id
        goods.setLastUpdateUserId(updateUserId);
        return baseMapper.updateById(goods);
    }

    @Override
    public PageUtil showGoodsByPage(PageUtil pageUtil) {
        IPage<YxCommonWarehouseGoods> page = new Page<>(pageUtil.getPageIndex(), pageUtil.getPageSize());
        IPage<YxCommonWarehouseGoods> GoodsPage = baseMapper.selectPage(page, null);
        pageUtil.setTotalSize((int) GoodsPage.getTotal());
        pageUtil.setData(GoodsPage);
        return pageUtil;
    }
}
